package com.wushuu.ce.ui.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.wushuu.ce.R;
import com.wushuu.ce.config.RequestParam;
import com.wushuu.ce.interfaces.IBaseView;
import com.wushuu.ce.interfaces.MyHandler;
import com.wushuu.ce.model.UserModel;
import com.wushuu.ce.present.LoginPresent;
import com.wushuu.ce.utils.StringUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

/**
 * ============================================================
 * <br/>
 * 作 者 :        xyb
 * <br/>
 * 版 本 :        1.0
 * <br/>
 * 创建日期 ：     2016-05-11 下午5:34
 * <br/>
 * 描 述 ：       登陆页面
 * <br/>
 * 修订历史 ：
 * <br/>
 * ============================================================
 **/
@ContentView(R.layout.activity_login)
public class LoginActivity extends BaseActivity implements IBaseView{

    @ViewInject(R.id.btn_login)
    private Button btnLogin;

    @ViewInject(R.id.et_login_username)
    private EditText etLoginName;

    @ViewInject(R.id.et_login_password)
    private EditText etLoginPwd;

    private String mLoginUserName = "";// 账号输入框内容
    private String mLoginPwd = "";//密码输入框内容

    @Override
    public void init() {

        etLoginName.addTextChangedListener(mTextWatcher);
        etLoginPwd.addTextChangedListener(mTextWatcher);

    }

    //TODO 抽象出来
    private TextWatcher mTextWatcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            mLoginUserName = etLoginName.getText().toString();
            mLoginPwd = etLoginPwd.getText().toString();
            setLoginButtonState();

        }
    };

    /****
     * 设置登录按钮状态
     */
    private void setLoginButtonState() {
        if (!StringUtils.isEmpty(mLoginUserName) && !StringUtils.isEmpty(mLoginPwd)) {
            btnLogin.setBackgroundColor(getResources().getColor(R.color.login_clicked_bkg));
        } else {
            btnLogin.setBackgroundColor(getResources().getColor(R.color.login_normal_bkg));
        }
    }



    @Event(value = R.id.btn_login, type = View.OnClickListener.class)
    private void btnClick(View view) {
        new LoginPresent(this).request(RequestParam.REQUEST_LOGIN);
    }

    @Override
    public Object getInfo(int code) {
        return new UserModel(mLoginUserName,mLoginPwd);
    }

    @Override
    public void setInfo(Object obj, int code) {
        startActivity(new Intent(LoginActivity.this,MainActivity.class));
    }

}
